from django.shortcuts import render
from django.views.generic import View
from .models import CourseOrg,CityDict
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
# Create your views here.

class OrgView(View):
    def get(self,request):
        #机构
        all_orgs=CourseOrg.objects.all()
        #热门机构
        hot_orgs = all_orgs.order_by("-click_nums")[:3]
        #城市
        all_citys = CityDict.objects.all()

        #根据城市筛选机构
        city_id = request.GET.get('city', "")
        if city_id:
            all_orgs = all_orgs.filter(city_id=int(city_id))

        # 根据类别筛选机构
        category = request.GET.get('ct', "")
        if category:
            all_orgs = all_orgs.filter(category=category)

        sort = request.GET.get('sort',"")
        if sort:
            if sort == "students":
                all_orgs = all_orgs.order_by("-students")
            elif sort == "courses":
                all_orgs = all_orgs.order_by("-course_nums")
        org_nums = all_orgs.count()
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:#导入包
            page = 1
        p = Paginator(all_orgs, 3,request=request)
        orgs = p.page(page)
        current_nav = 'org'
        return render(request,'org-list.html',{
            "page_obj":orgs,
            "all_citys": all_citys,
            "org_nums": org_nums,
            "city_id": city_id,
            "category": category,
            "hot_orgs": hot_orgs,
            "sort": sort,
            'current_nav': current_nav,
        })